from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore, QtGui,Qt,QtWidgets

import top
import window
import sys
import json2db
import datetime

class mainWindow(window.MainWindow):
    def __init__(self):
        super(mainWindow,self).__init__()
        self.__current_date = QtCore.QDate(1000,1,1)
        self.__current_news = []
        
        self.export_btn.clicked.connect(self.export_to_excel)
        self.update_btn.clicked.connect(self.update)
        self.calendarWidget.clicked.connect(self.on_calendarWidget_clicked)
        self.treeView.clicked.connect(self.on_treeView_clicked)
        self.today_btn.clicked.connect(self.on_today_btn_clicked)
        
        self.model = QtGui.QStandardItemModel(0,3,self)
        self.model.setHorizontalHeaderLabels(['序号','标题','网址'])
        self.treeView.setModel(self.model)
        self.treeView.show()
        
        #设置初始开始时间和结束时间
        self.sdate.setDate(QtCore.QDate(2002,9,8))
        self.edate.setDate(QtCore.QDate(2021,11,27))
        
    
    def __to_date(self,date):
        return datetime.datetime(date.year(),date.month(),date.day())
    
    def on_today_btn_clicked(self):
        date = QtCore.QDate().currentDate()
        self.calendarWidget.setSelectedDate(date)
        self.calendarWidget.clicked.emit(date)
        
        print('today')
    
    def on_treeView_clicked(self,index):
        row = self.model.itemFromIndex(index).row()
        #print(row)
        self.textBrowser.setText(self.__current_news[row][3])
    
    def on_calendarWidget_clicked(self,date):
        self.__current_date = date
        self.model.clear()
        
        date = self.__to_date(date)
        news = json2db.get_from_db(date,date)
        
        if news == None:
            self.textBrowser.setText('无历史数据')
            #要不要做点击以往的日子，爬取以往的新闻？
            return
            
        self.model.setHorizontalHeaderLabels(['序号','标题','网址'])
        
        self.__current_news = news[1:]
        for item in news[1:]:
            self.model.appendRow([QtGui.QStandardItem(str(item[1])),QtGui.QStandardItem(item[2]),QtGui.QStandardItem(item[4])])
        
    
    def export_to_excel(self):
        sdate,edate = self.sdate.date(),self.edate.date()
        sday,eday = self.__to_date(sdate),self.__to_date(edate)
        
        file_path = QtWidgets.QFileDialog.getSaveFileName(self,'export to excel','outfile.xlsx','Excel(*.xlsx)')[0]
        
        if file_path == '':
            return
        
        print('file_path:',file_path)
        top.outdata(sday,eday,file_path)
        QtWidgets.QMessageBox.information(self,'导出','导出成功')
        print(sday.__format__('%Y%m%d'),eday.__format__('%Y%m%d'))
        
    def update(self):
        top.getwebdata()
        print('update succeed')
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = mainWindow()
    
    MainWindow.show()
    sys.exit(app.exec_())